package com.htfyun.factorytest;

import android.graphics.Color;
import android.os.Bundle;
import android.os.SystemProperties;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.htfyun.factorytest.common.Constants;
import com.htfyun.eink.EinkUtils;

import com.htfyun.factorytest.R;

public class ScreenOffsetActivity extends SingleTestBaseActivity implements View.OnClickListener {

    private LinearLayout linearLayoutContainer;
    private int currentColorPos;
    private final static int MAX_COUNT = 2;
    private Button mAdjustment, mUndo, mQuit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screen_offset);

        linearLayoutContainer = findViewById(R.id.linearLayoutContainer);
        linearLayoutContainer.setBackgroundColor(Color.WHITE);

        linearLayoutContainer.setOnClickListener(this);

        mAdjustment = findViewById(R.id.adjustment);
        mUndo = findViewById(R.id.undo);
        mQuit = findViewById(R.id.quit);

        mAdjustment.setOnClickListener(this);
        mUndo.setOnClickListener(this);
        mQuit.setOnClickListener(this);
    }

    protected void showFinishDialog() {
        final String title = getString(R.string.ft_screen_offset_dialog_title);
        final String msg = getString(R.string.item_screen_offset);
        ViewDialogFragment dialog = ViewDialogFragment.newInstance(title, msg, true);

        dialog.show(getFragmentManager(),
                this::finishWithOK,
                this::finishWithFail);
    }

    protected void finishWithOK() {
        setResult(RESULT_OK);
//        SystemProperties.set(Constants.SCREEN_OFFSET, "0");
        ActivityCompat.finishAfterTransition(this);
    }

    protected void finishWithFail() {
        setResult(RESULT_CANCELED);
        ActivityCompat.finishAfterTransition(this);
    }

    @Override
    public void onClick(View view) {
        if (view == linearLayoutContainer) {
            switchBackground();
        } else if (view == mAdjustment) {
            SystemProperties.set(Constants.SCREEN_OFFSET, "0");
            switchBackground();
        } else if (view == mUndo) {
            if ("0".equals(SystemProperties.get(Constants.SCREEN_OFFSET, "4"))) {
                SystemProperties.set(Constants.SCREEN_OFFSET, "4");
                switchBackground();
            }
        } else if (view == mQuit) {
            finishWithOK();
        }
    }

    private void switchBackground() {
        currentColorPos++;
        if (currentColorPos >= MAX_COUNT) {
            currentColorPos = 0;
//                showFinishDialog();
        }
        linearLayoutContainer.setBackgroundColor(currentColorPos == 0 ? Color.WHITE : ContextCompat.getColor(this, R.color.gray_level_8));
        EinkUtils.forceEinkFullUpdateWithView(linearLayoutContainer);
/*            if (currentColorPos == 0) {
                linearLayoutContainer.removeAllViews();
                linearLayoutContainer.setBackgroundColor(Color.WHITE);

            } else if (currentColorPos == 1) {
                linearLayoutContainer.removeAllViews();
                linearLayoutContainer.setBackgroundColor(Color.BLACK);

            }*/
    }
}
